package com.bt.springboot.demo.io;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author zkx
 * @Date 2024/6/13 16:52
 */
public class BIODemo {

	public static void main(String[] args) throws Exception {
		ServerSocket serverSocket = new ServerSocket(8888,1024,null);
		System.out.println("启动服务器");
		while (true) {
			System.out.println("等待客户端连接");
			Socket socket = serverSocket.accept();
			System.out.println(socket.getRemoteSocketAddress());
			OutputStream outputStream = socket.getOutputStream();
			BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
			bufferedWriter.write(buildHttpResp());
			bufferedWriter.flush();
		}
	}

	private static String buildHttpResp(){
		return "<html><body><h1>Hello World</h1></body></html>" +
				"HTTP/1.1 200 OK\r\n" +
				"Content-Type: text/html\r\n" +
				"connection: Close\r\n" +
				"\r\n" +
				""
				;
	}
}
